package com.neurosky.thinkgear;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

public class TGDevice
{
    public static final int STATE_IDLE = 0;
    public static final int STATE_CONNECTING = 1;
    public static final int STATE_CONNECTED = 2;
    public static final int STATE_DISCONNECTED = 3;
    public static final int STATE_NOT_FOUND = 4;
    public static final int STATE_ERR_NO_DEVICE = 5;
    public static final int STATE_ERR_BT_OFF = 6;
    public static final int MSG_STATE_CHANGE = 256;
    public static final int MSG_POOR_SIGNAL = 2;
    public static final int MSG_HEART_RATE = 3;
    public static final int MSG_ATTENTION = 4;
    public static final int MSG_MEDITATION = 5;
    public static final int MSG_EGO_TRIM = 8;
    public static final int MSG_ZONE = 14;
    public static final int MSG_BLINK = 22;
    public static final int MSG_RELAXATION = 24;
    public static final int MSG_RESPIRATION = 25;
    public static final int MSG_POSITIVITY = 26;
    public static final int MSG_FAMILIARITY = 27;
    public static final int MSG_DIFFICULTY = 28;
    public static final int MSG_RAW_DATA = 128;
    public static final int MSG_EEG_POWER = 131;
    public static final int MSG_RAW_MULTI = 145;
    public static final int MSG_THINKCAPBETA_RAW = 176;
    public static final int MSG_THINKCAP_RAW = 177;
    public static final int MSG_SLEEP_STAGE = 178;
    public static final int MSG_MULTI_SIGNAL_STATUS = 179;
    public static final int MSG_RAW_COUNT = 19;
    public static final int MSG_LOW_BATTERY = 20;
    public static final int MSG_EKG_IDENTIFIED = 268;
    public static final int MSG_EKG_TRAINED = 269;
    public static final int MSG_EKG_TRAIN_STEP = 270;
    public static final int MSG_EKG_RRINT = 271;
    public static final int MSG_HEART_AGE = 272;
    public static final int MSG_HEART_AGE_5MIN = 273;
    public int ekgState = 0;
    public static final boolean L = true;
    public static boolean ekgPersonalizationEnabled = false;
    private static final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    Handler handler;
    private boolean byteLogEnabled = false;
    private boolean logEnabled = false;
    private boolean rawEnabled = false;
    private boolean isReadable;
    private boolean isStart = false;
    private int state;
    private BluetoothAdapter btAdapter;
    private OutputStream output;
    private BluetoothDevice connectedDevice;
    private Set<BluetoothDevice> pairedDevices;
    private ConnectThread btConnectThread;
    private ConnectedThread btConnectedThread;
    private OutputStream logWriter;
    private OutputStream byteLogWriter;
    public EkgSenseManager ekgSenseMan;
    private List<Integer> rrintBuffer;
    private List<Integer> rrintBufferForHeartAge;
    private List<Integer> inputRawBuffer;
    private StreamThread streamThread;
    private InputStream inputstream;
    public int inputAge;
    public int pass_seconds;
    private static int pass_raw_counter = 0;
    public static final int version = 22;
    public static final int minorversion = 106;
    public static final String build_title = "2012-12-14 SDK Release";

    public TGDevice(BluetoothAdapter btAdapter, Handler handler)
    {
        this.btAdapter = btAdapter;
        this.handler = handler;
        this.state = 0;
        if (ekgPersonalizationEnabled) {
            this.ekgSenseMan = new EkgSenseManager(this);
        }
        this.rrintBuffer = new ArrayList();
        this.rrintBufferForHeartAge = new ArrayList();
        this.inputRawBuffer = new ArrayList();

        logTheVersion();
        if (this.btAdapter.getState() == 10)
        {
            setState(6);
            this.state = 0;
        }
    }

    public TGDevice(InputStream s, OutputStream o, Handler handler)
    {
        this.state = 0;
        if (s == null) {
            Log.v("TGDevice", "Invalid input stream");
        }
        this.inputstream = s;
        if (o != null) {
            this.output = o;
        }
        this.handler = handler;
        if (ekgPersonalizationEnabled) {
            this.ekgSenseMan = new EkgSenseManager(this);
        }
        this.rrintBuffer = new ArrayList();
        this.rrintBufferForHeartAge = new ArrayList();
        this.inputRawBuffer = new ArrayList();

        logTheVersion();
    }

    public static int getVersion()
    {
        return 22;
    }

    public static int getMinorVersion()
    {
        return 106;
    }

    public static String getTitle()
    {
        return "2012-12-14 SDK Release";
    }

    private static void logTheVersion()
    {
        Log.d("TGDevice", "Initialized. Version: " + getVersion());
        if (getVersion() != TGconfig.getVersion()) {
            Log.w("TGDevice", "OOPS!! TG-SDK internal Version mismatch: TGDevice: "
                    + getVersion() + " TGconfig: "
                    + TGconfig.getVersion() + "\n");
        }
    }

    public synchronized void connect(BluetoothDevice btDevice)
    {
        connect(btDevice, false);
    }

    public synchronized void connect(BluetoothDevice btDevice, boolean rawEnabled)
    {
        if (this.btAdapter.getState() == 10)
        {
            setState(6);
            return;
        }
        setStart(false);
        this.rawEnabled = rawEnabled;
        Set<BluetoothDevice> d;
        (d = new HashSet()).add(btDevice);
        if (this.btConnectThread != null)
        {
            this.btConnectThread.cancel();
            this.btConnectThread = null;
        }
        this.btConnectThread = new ConnectThread(d);///////
        this.btConnectThread.start();
        setState(1);
        Log.i("TGDevice", "Connecting to device: "
                + btDevice.getName() + " with raw enabled: "
                + rawEnabled);
    }

    public synchronized void connect(boolean rawEnabled)
    {
        if (this.btAdapter.getState() == 10)
        {
            setState(6);
            return;
        }
        if (this.btAdapter != null)
        {
            setStart(false);
            this.rawEnabled = rawEnabled;
            this.pairedDevices = this.btAdapter.getBondedDevices();
            if (this.pairedDevices.isEmpty()) {
                if (this.btConnectThread != null)
                {
                    this.btConnectThread.cancel();
                    this.btConnectThread = null;
                }
            }
            this.btConnectThread = new ConnectThread(this.pairedDevices);
            this.btConnectThread.start();
            setState(1);
        }
    }

    /**
     * @deprecated
     */
    public synchronized void connectUart(boolean rawEnabled)
    {
        connectStream(rawEnabled);
    }

    public synchronized void connectStream(boolean paramBoolean)
    {
        if (this.streamThread != null)
        {
            this.streamThread.cancel();
            this.streamThread = null;
        }
        this.streamThread = new StreamThread(this.inputstream);
        this.streamThread.start();
        setState(2);
        setReadable(true);
    }

    private synchronized void connected(BluetoothSocket btSocket)
    {
        if (this.btConnectThread != null) {
            this.btConnectThread = null;
        }
        if (this.btConnectedThread != null)
        {
            this.btConnectedThread.cancel();
            this.btConnectedThread = null;
        }
        setReadable(true);

        this.btConnectedThread = new ConnectedThread(btSocket, this.rawEnabled);
        this.btConnectedThread.start();
        setState(2);
    }

    public synchronized void close()
    {
        if (this.btConnectThread != null)
        {
            this.btConnectThread.cancel();
            this.btConnectThread = null;
        }
        if (this.btConnectedThread != null)
        {
            this.btConnectedThread.cancel();
            this.btConnectedThread = null;
        }
        if (this.streamThread != null)
        {
            this.streamThread.cancel();
            this.streamThread = null;
        }
        stopLog();
        setState(3);
        Log.i("TGDevice", "Closing connections");
    }

    public synchronized void start()
    {
        Log.d("TGDevice", "Starting stream");
        setStart(true);
    }

    public synchronized void stop()
    {
        Log.d("TGDevice", "Stopping stream");
        setStart(false);
        setState(0);
    }

    public BluetoothDevice getConnectedDevice()
    {
        return this.connectedDevice;
    }

    private synchronized void setState(int s)
    {
        this.state = s;
        this.handler.obtainMessage(256, this.state, 0).sendToTarget();
    }

    public synchronized int getState()
    {
        return this.state;
    }

    private synchronized void setReadable(boolean readable)
    {
        this.isReadable = readable;
    }

    private synchronized boolean getReadable()
    {
        return this.isReadable;
    }

    private synchronized void setStart(boolean start)
    {
        this.isStart = start;
    }

    private synchronized boolean getStart()
    {
        return this.isStart;
    }

    public boolean startLog(String path, String fileName)
    {
        if (this.logEnabled) {
            return false;
        }
        String state = Environment.getExternalStorageState();
        if ("mounted".equals(state))
        {
            if (path == "") {
                path = Environment.getExternalStorageDirectory() +
                        "/Android/data/com.neurosky.thinkgear/files";
            }
            if (fileName == "") {
                fileName = DateFormat.format("yyyy-MM-dd-hhmmss", new Date()).toString() + ".txt";
            }
            File dir = new File(path);
            File file = new File(path, fileName);
            try
            {
                if ((!dir.exists()) &&
                        (dir.mkdirs())) {
                    Log.d("TGDevice", "Log directory created successfully");
                }
                if (!file.exists()) {
                    file.createNewFile();
                }
                if (file.exists())
                {
                    this.logWriter = new FileOutputStream(file);
                    this.logEnabled = true;

                    Log.i("TGDevice", "Logging enabled: " + path + fileName);
                }
            }
            catch (IOException e)
            {
                Log.v("TGDevice", "WTF: " + e + " path: " + path);
                return false;
            }
            return true;
        }
        return false;
    }

    public boolean startByteLog(String path, String fileName)
    {
        if (this.byteLogEnabled) {
            return false;
        }
        String state = Environment.getExternalStorageState();
        if ("mounted".equals(state))
        {
            if (path == "") {
                path = Environment.getExternalStorageDirectory() +
                        "/Android/data/com.neurosky.thinkgear/files";
            }
            if (fileName == "") {
                fileName = DateFormat.format("yyyy-MM-dd-hhmmss", new Date()).toString() + ".byte.txt";
            }
            File dir = new File(path);
            File file = new File(path, fileName);
            try
            {
                if ((!dir.exists()) &&
                        (dir.mkdirs())) {
                    Log.d("TGDevice", "Log directory created successfully");
                }
                if (!file.exists()) {
                    file.createNewFile();
                }
                if (file.exists())
                {
                    this.byteLogWriter = new FileOutputStream(file);
                    this.byteLogEnabled = true;

                    Log.i("TGDevice", " Byte Logging enabled: " + path + fileName);
                }
            }
            catch (IOException e)
            {
                Log.v("TGDevice", "WTF: " + e + " path: " + path);
                return false;
            }
            return true;
        }
        return false;
    }

    public void stopLog()
    {
        IOException localIOException1;
        if (this.logEnabled)
        {
            this.logEnabled = false;
            try
            {
                this.logWriter.close();
                Log.i("TGDevice", "Logging disabled");
            }
            catch (IOException localIOException2)
            {
                (localIOException1 = localIOException2).printStackTrace();
            }
        }
        if (this.byteLogEnabled)
        {
            this.byteLogEnabled = false;
            try
            {
                this.byteLogWriter.close();
                Log.i("TGDevice", "Logging disabled"); return;
            }
            catch (IOException localIOException3)
            {
                (localIOException1 = localIOException3).printStackTrace();
            }
        }
    }

    private class ConnectThread extends Thread
    {
        private int SCAN_TIMEOUT = 5000;
        private Set<BluetoothDevice> btDevices;
        private BluetoothSocket btSocket;
        private TGDevice.TGParser parser;
        private InputStream inStream;
        private int apiVersion;

        public ConnectThread()
        {
            this.btDevices = devices;
            this.apiVersion = Integer.valueOf(Build.VERSION.SDK).intValue();

            this.parser = new TGDevice.TGParser(TGDevice.this, null, false);
        }

        public final void run()
        {
            Looper.prepare();
            Log.v("TGDevice", "Begin ConnectThread");
            setName("ConnectThread");
            if (TGDevice.this.btAdapter.isDiscovering()) {
                TGDevice.this.btAdapter.cancelDiscovery();
            }
            if (this.btDevices.size() == 0)
            {
                TGDevice.this.setState(5);
                return;
            }
            for (BluetoothDevice d : this.btDevices)
            {
                Log.d("TGDevice", "Trying to connect to: " + d.getName());
                try
                {
                    if ((this.apiVersion >= 5) && (this.apiVersion < 10)) {
                        this.btSocket = d.createRfcommSocketToServiceRecord(TGDevice.uuid);
                    } else if (this.apiVersion >= 10) {
                        this.btSocket = d.createInsecureRfcommSocketToServiceRecord(TGDevice.uuid);
                    }
                }
                catch (IOException e)
                {
                    Log.d("TGDevice", "Error creating socket: " + e);
                }
                continue;
                try
                {
                    this.btSocket.connect();
                }
                catch (IOException localIOException1)
                {
                    Log.d("TGDevice", "Could not establish a connection with: " + d.getName());
                    try
                    {
                        this.btSocket.close();
                    }
                    catch (IOException localIOException2) {}
                }
                continue;
                try
                {
                    this.inStream = this.btSocket.getInputStream();
                }
                catch (IOException localIOException3)
                {
                    Log.d("TGDevice", "Could not get input stream");
                }
                int pState = 0;
                byte[] buffer = new byte[1];
                long start = System.currentTimeMillis();
                do
                {
                    try
                    {
                        this.inStream.read(buffer);
                        pState = this.parser.parseByte(buffer[0]);
                    }
                    catch (IOException localIOException4) {}
                } while ((pState < 5) && (System.currentTimeMillis() - start < this.SCAN_TIMEOUT));
                if (pState == 5)
                {
                    TGDevice.this.connectedDevice = d;

                    TGDevice.this.connected(this.btSocket);

                    return;
                }
                Log.d("TGDevice", d.getName() + " is not a valid device.");
            }
            TGDevice.this.setState(4);
            Looper.loop();
        }

        public final void cancel()
        {
            try
            {
                if (this.btSocket != null) {
                    this.btSocket.close();
                }
                return;
            }
            catch (IOException localIOException) {}
        }
    }

    private class ConnectedThread extends Thread
    {
        private BluetoothSocket btSocket;
        private InputStream inStream;
        private TGDevice.TGParser parser;

        public ConnectedThread(BluetoothSocket s, boolean rawEnabled)
        {
            this.btSocket = s;
            this.parser = new TGDevice.TGParser(TGDevice.this, TGDevice.this.handler, rawEnabled);
        }

        /* Error */
        public final void run()
        {
            // Byte code:
            //   0: ldc 4
            //   2: ldc 1
            //   4: invokestatic 22	android/util/Log:v	(Ljava/lang/String;Ljava/lang/String;)I
            //   7: pop
            //   8: aload_0
            //   9: ldc 2
            //   11: invokevirtual 29	com/neurosky/thinkgear/TGDevice$ConnectedThread:setName	(Ljava/lang/String;)V
            //   14: aload_0
            //   15: aload_0
            //   16: getfield 15	com/neurosky/thinkgear/TGDevice$ConnectedThread:btSocket	Landroid/bluetooth/BluetoothSocket;
            //   19: invokevirtual 20	android/bluetooth/BluetoothSocket:getInputStream	()Ljava/io/InputStream;
            //   22: putfield 16	com/neurosky/thinkgear/TGDevice$ConnectedThread:inStream	Ljava/io/InputStream;
            //   25: aload_0
            //   26: getfield 18	com/neurosky/thinkgear/TGDevice$ConnectedThread:this$0	Lcom/neurosky/thinkgear/TGDevice;
            //   29: aload_0
            //   30: getfield 15	com/neurosky/thinkgear/TGDevice$ConnectedThread:btSocket	Landroid/bluetooth/BluetoothSocket;
            //   33: invokevirtual 21	android/bluetooth/BluetoothSocket:getOutputStream	()Ljava/io/OutputStream;
            //   36: invokestatic 25	com/neurosky/thinkgear/TGDevice:access$502	(Lcom/neurosky/thinkgear/TGDevice;Ljava/io/OutputStream;)Ljava/io/OutputStream;
            //   39: pop
            //   40: iconst_1
            //   41: newarray <illegal type>
            //   43: astore_1
            //   44: aload_0
            //   45: getfield 18	com/neurosky/thinkgear/TGDevice$ConnectedThread:this$0	Lcom/neurosky/thinkgear/TGDevice;
            //   48: invokestatic 26	com/neurosky/thinkgear/TGDevice:access$600	(Lcom/neurosky/thinkgear/TGDevice;)Z
            //   51: ifeq +23 -> 74
            //   54: aload_0
            //   55: getfield 16	com/neurosky/thinkgear/TGDevice$ConnectedThread:inStream	Ljava/io/InputStream;
            //   58: aload_1
            //   59: invokevirtual 34	java/io/InputStream:read	([B)I
            //   62: pop
            //   63: aload_0
            //   64: getfield 17	com/neurosky/thinkgear/TGDevice$ConnectedThread:parser	Lcom/neurosky/thinkgear/TGDevice$TGParser;
            //   67: aload_1
            //   68: iconst_0
            //   69: baload
            //   70: invokevirtual 31	com/neurosky/thinkgear/TGDevice$TGParser:parseByte	(B)I
            //   73: pop
            //   74: aload_0
            //   75: getfield 18	com/neurosky/thinkgear/TGDevice$ConnectedThread:this$0	Lcom/neurosky/thinkgear/TGDevice;
            //   78: invokestatic 27	com/neurosky/thinkgear/TGDevice:access$700	(Lcom/neurosky/thinkgear/TGDevice;)Z
            //   81: ifne -37 -> 44
            //   84: aload_0
            //   85: getfield 16	com/neurosky/thinkgear/TGDevice$ConnectedThread:inStream	Ljava/io/InputStream;
            //   88: invokevirtual 33	java/io/InputStream:close	()V
            //   91: aload_0
            //   92: getfield 18	com/neurosky/thinkgear/TGDevice$ConnectedThread:this$0	Lcom/neurosky/thinkgear/TGDevice;
            //   95: invokestatic 24	com/neurosky/thinkgear/TGDevice:access$500	(Lcom/neurosky/thinkgear/TGDevice;)Ljava/io/OutputStream;
            //   98: invokevirtual 35	java/io/OutputStream:close	()V
            //   101: aload_0
            //   102: getfield 15	com/neurosky/thinkgear/TGDevice$ConnectedThread:btSocket	Landroid/bluetooth/BluetoothSocket;
            //   105: invokevirtual 19	android/bluetooth/BluetoothSocket:close	()V
            //   108: return
            //   109: pop
            //   110: return
            //   111: astore_1
            //   112: aload_0
            //   113: getfield 18	com/neurosky/thinkgear/TGDevice$ConnectedThread:this$0	Lcom/neurosky/thinkgear/TGDevice;
            //   116: iconst_3
            //   117: invokestatic 23	com/neurosky/thinkgear/TGDevice:access$100	(Lcom/neurosky/thinkgear/TGDevice;I)V
            //   120: aload_0
            //   121: getfield 15	com/neurosky/thinkgear/TGDevice$ConnectedThread:btSocket	Landroid/bluetooth/BluetoothSocket;
            //   124: invokevirtual 19	android/bluetooth/BluetoothSocket:close	()V
            //   127: return
            //   128: pop
            //   129: return
            //   130: astore_1
            //   131: aload_0
            //   132: getfield 15	com/neurosky/thinkgear/TGDevice$ConnectedThread:btSocket	Landroid/bluetooth/BluetoothSocket;
            //   135: invokevirtual 19	android/bluetooth/BluetoothSocket:close	()V
            //   138: goto +4 -> 142
            //   141: pop
            //   142: aload_1
            //   143: athrow
            // Line number table:
            //   Java source line #771	-> byte code offset #0
            //   Java source line #772	-> byte code offset #8
            //   Java source line #775	-> byte code offset #14
            //   Java source line #776	-> byte code offset #25
            //   Java source line #778	-> byte code offset #40
            //   Java source line #780	-> byte code offset #44
            //   Java source line #781	-> byte code offset #54
            //   Java source line #782	-> byte code offset #63
            //   Java source line #785	-> byte code offset #74
            //   Java source line #787	-> byte code offset #84
            //   Java source line #788	-> byte code offset #91
            //   Java source line #796	-> byte code offset #101
            //   Java source line #798	-> byte code offset #108
            //   Java source line #797	-> byte code offset #109
            //   Java source line #800	-> byte code offset #110
            //   Java source line #790	-> byte code offset #111
            //   Java source line #791	-> byte code offset #112
            //   Java source line #796	-> byte code offset #120
            //   Java source line #798	-> byte code offset #127
            //   Java source line #797	-> byte code offset #128
            //   Java source line #798	-> byte code offset #129
            //   Java source line #795	-> byte code offset #130
            //   Java source line #796	-> byte code offset #131
            //   Java source line #798	-> byte code offset #138
            //   Java source line #797	-> byte code offset #141
            //   Java source line #798	-> byte code offset #142
            // Local variable table:
            //   start	length	slot	name	signature
            //   0	132	0	this	ConnectedThread
            //   43	25	1	buffer	byte[]
            //   111	32	1	localIOException	IOException
            //   109	1	3	localIOException1	IOException
            //   128	1	4	localIOException2	IOException
            //   141	1	5	localIOException3	IOException
            // Exception table:
            //   from	to	target	type
            //   101	108	109	java/io/IOException
            //   14	101	111	java/io/IOException
            //   120	127	128	java/io/IOException
            //   14	101	130	finally
            //   111	120	130	finally
            //   131	138	141	java/io/IOException
        }

        public final void cancel()
        {
            try
            {
                Log.v("TGDevice", "ConnectedThreadEnding");
                this.inStream.close();
                TGDevice.this.output.close();
                this.btSocket.close();
            }
            catch (IOException localIOException2)
            {
                IOException localIOException1;
                (localIOException1 = localIOException2).printStackTrace();
            }
            TGDevice.this.setReadable(false);
        }
    }

    private class StreamThread extends Thread
    {
        private TGDevice.TGParser parser;
        private InputStream input;
        private BufferedInputStream buff;
        private int r = -1;
        private int availableBytes = 0;

        public StreamThread(InputStream i)
        {
            this.parser = new TGDevice.TGParser(TGDevice.this, TGDevice.this.handler, true);
            this.input = i;
            this.buff = new BufferedInputStream(this.input);
        }

        public final void run()
        {
            if (this.input == null) {
                Log.v("TGDevice", "UART ERROR");
            }
            try
            {
                do
                {
                    if (TGDevice.this.getStart())
                    {
                        this.availableBytes = this.buff.available();
                        while (this.availableBytes > 0)
                        {
                            this.r = this.buff.read();
                            if (this.r >= 0) {
                                this.parser.parseByte((byte)this.r);
                            }
                            this.availableBytes -= 1;
                        }
                        this.r = -1;
                    }
                    try
                    {
                        Thread.sleep(1L, 500000);
                    }
                    catch (InterruptedException localInterruptedException2)
                    {
                        InterruptedException localInterruptedException1;
                        (localInterruptedException1 = localInterruptedException2).printStackTrace();
                    }
                } while (TGDevice.this.getReadable());
                this.input.close(); return;
            }
            catch (IOException localIOException)
            {
                TGDevice.this.setState(5);
            }
        }

        public final void cancel()
        {
            TGDevice.this.setReadable(false);
            try
            {
                this.input.close();
            }
            catch (IOException localIOException2)
            {
                IOException localIOException1;
                (localIOException1 = localIOException2).printStackTrace();
            }
            Log.v("TGDevice", "StreamThread ending");
        }
    }

    private class TGParser
    {
        private Handler handler;
        private boolean hwHeartRateEnabled = false;
        private boolean rawCountEnabled = false;
        private boolean sineWaveEnabled = false;
        private boolean trimByteSent = false;
        private boolean isBMD101 = true;
        private boolean respirationEnabled = true;
        private boolean blinkEnabled = false;
        private boolean sleepStageEnabled = false;
        private boolean relaxationEnabled = true;
        private boolean rawEnabled;
        private boolean familiarityEnabled = false;
        private boolean difficultyEnabled = false;
        private boolean timeBasedIntervalEnabled = true;
        private boolean calculationEnabled = false;
        private int parserState;
        private int payloadLength;
        private int payloadBytesReceived;
        private int payloadSum;
        private int checksum;
        private int rawCount = 0;
        private byte[] payload = new byte['?'];
        private int[] sine = new int['?'];
        private double[] coeff = { -9.7440442358423E-5D, -1.1588027064266E-4D, -1.68411968763922E-4D, -2.20230946712853E-4D,
                -2.61497617618105E-4D, -2.80928519745322E-4D, -2.66503975879537E-4D, -2.07456105101242E-4D,
                -9.60607754678275E-5D, 7.06479844998841E-5D, 2.89279202398974E-4D, 5.49359171310485E-4D,
                8.33627498869421E-4D, 0.00111885157865328D, 0.0013781195000037D, 0.00158354624155544D,
                0.00170981291692959D, 0.00173740932884499D, 0.00165575166183813D, 0.00146506438032912D,
                0.00117746477759056D, 8.16034117695199E-4D, 4.12844215447331E-4D, 5.24895417994034E-6D,
                -3.68302018467704E-4D, -6.73193852496632E-4D, -8.82833781720179E-4D, -9.82353338460868E-4D,
                -9.70341482030938E-4D, -8.59260495804291E-4D, -6.73493754692806E-4D, -4.46146123317908E-4D,
                -2.14182662702644E-4D, -1.34624881903889E-5D, 1.26516426775505E-4D, 1.86466300968715E-4D,
                1.60107470556599E-4D, 5.45352349753129E-5D, -1.10987588308181E-4D, -3.08509874719252E-4D,
                -5.05715502523139E-4D, -6.71317451493218E-4D, -7.79750742743636E-4D, -8.15365828849824E-4D,
                -7.74523301469025E-4D, -6.66185490192942E-4D, -5.10005871163562E-4D, -3.33137954084399E-4D,
                -1.65400913899566E-4D, -3.45312065837977E-5D, 3.86308033064396E-5D, 4.32881937489408E-5D,
                -1.94228353791984E-5D, -1.37026795703571E-4D, -2.87767026596535E-4D, -4.44674887956967E-4D,
                -5.80032844849327E-4D, -6.7039277429577E-4D, -7.00350679062249E-4D, -6.65360053537445E-4D,
                -5.72186532290299E-4D, -4.37843807718034E-4D, -2.86293068422389E-4D, -1.44403231408916E-4D,
                -3.69617246459944E-5D, 1.74584888808446E-5D, 9.98070420727893E-6D, -5.7277674807016E-5D,
                -1.71410449954226E-4D, -3.11303641234571E-4D, -4.51218621755097E-4D, -5.65653686021656E-4D,
                -6.33691820405254E-4D, -6.42981223594934E-4D, -5.9171601394605E-4D, -4.89172991016836E-4D,
                -3.5379923287896E-4D, -2.10096448291764E-4D, -8.39458869442319E-5D, 1.8664996724834E-6D,
                3.20306720584621E-5D, 1.28683042804457E-6D, -8.43683732439765E-5D, -2.08973511855545E-4D,
                -3.49264936862277E-4D, -4.79138906517235E-4D, -5.74195668379932E-4D, -6.1644929580642E-4D,
                -5.97431437802573E-4D, -5.20018323470526E-4D, -3.97735963103583E-4D, -2.5255457870159E-4D,
                -1.10666382579362E-4D, 2.08912857755044E-6D, 6.52221969033149E-5D, 6.71888260706398E-5D,
                7.8132740985481E-6D, -1.01816886829692E-4D, -2.41089628777638E-4D, -3.83829338777272E-4D,
                -5.02871887665066E-4D, -5.75313621310307E-4D, -5.86569142963063E-4D, -5.33373583604141E-4D,
                -4.24271089806838E-4D, -2.78364015955548E-4D, -1.21647686322812E-4D, 1.75247095049829E-5D,
                1.13908852304928E-4D, 1.49870978174831E-4D, 1.18971164016274E-4D, 2.70624688829392E-5D,
                -1.08442383216512E-4D, -2.61904280504075E-4D, -4.03915938898268E-4D, -5.06955565217396E-4D,
                -5.50312187666917E-4D, -5.24244802280253E-4D, -4.31708214571715E-4D, -2.88174788977175E-4D,
                -1.18683678206045E-4D, 4.64904244119377E-5D, 1.77638742212356E-4D, 2.50946901093621E-4D,
                2.5323129899956E-4D, 1.84316204896415E-4D, 5.74523943528654E-5D, -1.03198447100559E-4D,
                -2.66713802736679E-4D, -4.01313076289361E-4D, -4.80063170756953E-4D, -4.86191446429637E-4D,
                -4.16139661135309E-4D, -2.80623721390072E-4D, -1.02610485587946E-4D, 8.65917343263471E-5D,
                2.53285447950829E-4D, 3.67434261574489E-4D, 4.08543598287131E-4D, 3.69410870378961E-4D,
                2.57871342545317E-4D, 9.53324649004418E-5D, -8.68245287093865E-5D, -2.53184359458119E-4D,
                -3.70652605605774E-4D, -4.14892907150544E-4D, -3.74794893114052E-4D, -2.54954924592149E-4D,
                -7.48344639902692E-5D, 1.34455506846221E-4D, 3.36015790834792E-4D, 4.93770262973607E-4D,
                5.79439273810329E-4D, 5.77738539851612E-4D, 4.8963925032985E-4D, 3.3221821078459E-4D,
                1.35904911184024E-4D, -6.12246546286886E-5D, -2.20256642442962E-4D, -3.08920034466907E-4D,
                -3.07522853427615E-4D, -2.13033893593434E-4D, -3.96953994968743E-5D, 1.83180508682799E-4D,
                4.16678727825662E-4D, 6.19220331665681E-4D, 7.54513329299535E-4D, 7.982248319367E-4D,
                7.42921251348084E-4D, 5.99512281288621E-4D, 3.95685490536059E-4D, 1.70770556774819E-4D,
                -3.12520579316764E-5D, -1.70122935327124E-4D, -2.16702229081013E-4D, -1.58799772313772E-4D,
                -3.51663112894297E-6D, 2.23593185867299E-4D, 4.83230524813043E-4D, 7.2930778548967E-4D,
                9.17680849208474E-4D, 0.00101425160569053D, 0.00100170797231571D, 8.82824811935392E-4D,
                6.80444821457835E-4D, 4.33304948104277E-4D, 1.89211708760537E-4D, -3.97963442099545E-6D,
                -1.0714149456976E-4D, -9.75382510211861E-5D, 2.68551602843451E-5D, 2.464637750764E-4D,
                5.2364554256276E-4D, 8.09281106655558E-4D, 0.00105200093732304D, 0.00120759179058643D,
                0.00124758803351198D, 0.0011646592200388D, 9.74496584720574E-4D, 7.13031741195615E-4D,
                4.30193817156114E-4D, 1.80569079914348E-4D, 1.34194308769052E-5D, -3.68288426928038E-5D,
                4.29148478210438E-5D, 2.41435399013574E-4D, 5.25127275789142E-4D, 8.43769833829344E-4D,
                0.00113987720111445D, 0.00135918714255885D, 0.00146107399744823D, 0.00142619655393134D,
                0.00126063555002634D, 9.94960964833502E-4D, 6.79052557660961E-4D, 3.72855678084063E-4D,
                1.35466757159849E-4D, 1.3849309144843E-5D, 3.4012343541018E-5D, 1.95625958665316E-4D,
                4.71854489811893E-4D, 8.13805297511983E-4D, 0.00115954087382408D, 0.00144532357565324D,
                0.00161772585583283D, 0.00164363911682995D, 0.00151697631774312D, 0.00126005466552864D,
                9.20018434871427E-4D, 5.6019737374658E-4D, 2.48641831922795E-4D, 4.52472361597781E-5D,
                -9.44217447750788E-6D, 9.76836786773469E-5D, 3.49587730781208E-4D, 7.01646118277693E-4D,
                0.00108976947205013D, 0.00144196301098922D, 0.00169189034302864D, 0.00179124501034788D,
                0.00171926753558527D, 0.00148689997345125D, 0.00113538873580102D, 7.28840658240885E-4D,
                3.42704502465947E-4D, 4.9611695770945E-5D, -9.41422652072306E-5D, -5.95892868598273E-5D,
                1.49210026530823E-4D, 4.95251264637447E-4D, 9.15185152311649E-4D, 0.00133064261755464D,
                0.00166288067239408D, 0.00184740513073318D, 0.00184646637631842D, 0.00165640715611924D,
                0.00130904009040423D, 8.66067932635046E-4D, 4.08117020837066E-4D, 1.9730705769468E-5D,
                -2.26295960430978E-4D, -2.8293202316019E-4D, -1.37996600502361E-4D, 1.82850004396995E-4D,
                6.20484195280941E-4D, 0.00109274642643676D, 0.00150966475862766D, 0.00179008298063751D,
                0.00187719780614157D, 0.00174946519687166D, 0.00142535532397548D, 9.60366107007573E-4D,
                4.37330250865943E-4D, -4.88633815893146E-5D, -4.08761057604636E-4D, -5.75386700363658E-4D,
                -5.17038609916063E-4D, -2.43954622688529E-4D, 1.93088506220769E-4D, 7.11525817673556E-4D,
                0.0012121573011249D, 0.00159738209371754D, 0.00178971139213456D, 0.00174651275888789D,
                0.00146872604881992D, 0.00100124647033075D, 4.25335294937135E-4D, -1.56208575877302E-4D,
                -6.37884782523169E-4D, -9.31420112254212E-4D, -9.82504470494768E-4D, -7.81692899252715E-4D,
                -3.66775820220804E-4D, 1.83416169919086E-4D, 7.63296964727054E-4D, 0.00125980163155078D,
                0.00157365651799556D, 0.00163813734628643D, 0.00143228621248031D, 9.85476250082807E-4D,
                3.7285572048885E-4D, -2.98152986619019E-4D, -9.07384243766655E-4D, -0.00134444209695172D,
                -0.00152949276953303D, -0.00142891708711331D, -0.00106227941833982D, -4.99669263582827E-4D,
                1.50755304146288E-4D, 7.6234087929536E-4D, 0.00121357089114537D, 0.00141088442354037D,
                0.00130687364779612D, 9.0986693888556E-4D, 2.83438836206569E-4D, -4.64720243596698E-4D,
                -0.00120218142834308D, -0.00179638061831811D, -0.00213943521160232D, -0.00216898578250211D,
                -0.00188060329600902D, -0.00132975088840251D, -6.22282918266146E-4D, 1.0447037192244E-4D,
                7.07100966540956E-4D, 0.00106288579277491D, 0.0010933599394102D, 7.79759007376738E-4D,
                1.67731567404481E-4D, -6.40203673959743E-4D, -0.00150281291712562D, -0.00226613307010532D,
                -0.00279210409544492D, -0.00298494810943401D, -0.00280985876267867D, -0.00230077814015934D,
                -0.00155516788603045D, -7.16946449325894E-4D, 4.98425162577E-5D, 5.91427811460621E-4D,
                7.93553359180197E-4D, 6.03626371741223E-4D, 4.18882442035887E-5D, -8.01113927599773E-4D,
                -0.00178012805276415D, -0.00272159333227661D, -0.00345566817171523D, -0.00384837663407466D,
                -0.0038274287920596D, -0.00339713383295239D, -0.0026389926345583D, -0.00169797079597746D,
                -7.56362553657179E-4D, -3.02801614322377E-7D, 4.15303124638464E-4D, 3.96486626536007E-4D,
                -7.07582257228779E-5D, -9.16299236533598E-4D, -0.00199725651943966D, -0.00312350225351387D,
                -0.0040923984060442D, -0.00472678969339786D, -0.00490881515896487D, -0.0046034450441617D,
                -0.00386674837933375D, -0.00283736136973274D, -0.00171205950763413D, -7.10028821381479E-4D,
                -3.19766104447796E-5D, 1.78005205844006E-4D, -1.39079612299567E-4D, -9.4420432887318E-4D,
                -0.00210539663183083D, -0.00342058739997302D, -0.00465401417839112D, -0.00557995156612952D,
                -0.00602539931306045D, -0.00590398183887164D, -0.00523417542360424D, -0.00413837160798384D,
                -0.00282220492576368D, -0.00153786268861382D, -5.37603813784902E-4D, -2.62833612978657E-5D,
                -1.21501341208779E-4D, -8.29341117202341E-4D, -0.00204036227034438D, -0.00354738366789472D,
                -0.00508198134809249D, -0.00636358411753227D, -0.00715198450674306D, -0.00729374687426444D,
                -0.00675336186413425D, -0.00562315333865641D, -0.00410925127068627D, -0.0024958874386246D,
                -0.00109388518637658D, -1.82849419019012E-4D, 4.25002186405121E-5D, -4.88598112186092E-4D,
                -0.00170597869344896D, -0.0034077424198901D, -0.00529439806145769D, -0.00702249784257682D,
                -0.00826802752262929D, -0.00878816295614779D, -0.00846951380687305D, -0.00735360701803667D,
                -0.00563389951885155D, -0.00362421333211422D, -0.00170340034633871D, -2.46078432288275E-4D,
                4.48220493090697E-4D, 2.14038601094618E-4D, -9.44144342204741E-4D, -0.00284688065681757D,
                -0.00516735944741822D, -0.00748779521095554D, -0.00937485221013864D, -0.010460832484825D, -0.0105154409918717D, -0.00949451455636883D, -0.00755555220454338D, -0.00503604893255608D, -0.00239708312458895D, -1.41510749016328E-4D, 0.00127911047620282D, 0.00155177140920011D, 5.6704834171846E-4D, -0.00155130143843582D, -0.00445908005673155D, -0.00764634029517581D, -0.0105255608719074D, -0.0125385395087861D, -0.013262846112682D, -0.0124981540711494D, -0.0103153202185525D, -0.00705740100760807D, -0.00328988355713241D, 2.93142491633399E-4D, 0.00299195454090684D, 0.00423206494064704D, 0.00367942048382208D, 0.00131776653753629D, -0.00252808679982519D, -0.00723063563671066D, -0.0119562940256244D, -0.0158063443723257D, -0.0179793574883495D, -0.0179272006989418D, -0.0154760392566849D, -0.0108883680748677D, -0.00485085240159456D, 0.00161504006609049D, 0.00730955626257326D, 0.0110558916788708D, 0.0119147925193301D, 0.00937681513265424D, 0.00349822751127974D, -0.00504625235976391D, -0.0150084099659343D, -0.0247086729108038D, -0.0322590319016573D, -0.0358365930989115D, -0.0339679171620155D, -0.0257807599622063D, -0.0111828622087802D, 0.00906290466419837D, 0.0333830906953785D, 0.0595562582455106D, 0.0849741290731021D, 0.106966442254914D, 0.123144409178258D, 0.13171399483958D, 0.13171399483958D, 0.123144409178258D, 0.106966442254914D, 0.0849741290731021D, 0.0595562582455106D, 0.0333830906953785D, 0.00906290466419837D, -0.0111828622087802D, -0.0257807599622063D, -0.0339679171620155D, -0.0358365930989115D, -0.0322590319016573D, -0.0247086729108038D, -0.0150084099659343D, -0.00504625235976391D, 0.00349822751127974D, 0.00937681513265424D, 0.0119147925193301D, 0.0110558916788708D, 0.00730955626257326D, 0.00161504006609049D, -0.00485085240159456D, -0.0108883680748677D, -0.0154760392566849D, -0.0179272006989418D, -0.0179793574883495D, -0.0158063443723257D, -0.0119562940256244D, -0.00723063563671066D, -0.00252808679982519D, 0.00131776653753629D, 0.00367942048382208D, 0.00423206494064704D, 0.00299195454090684D, 2.93142491633399E-4D, -0.00328988355713241D, -0.00705740100760807D, -0.0103153202185525D, -0.0124981540711494D, -0.013262846112682D, -0.0125385395087861D, -0.0105255608719074D, -0.00764634029517581D, -0.00445908005673155D, -0.00155130143843582D, 5.6704834171846E-4D, 0.00155177140920011D, 0.00127911047620282D, -1.41510749016328E-4D, -0.00239708312458895D, -0.00503604893255608D, -0.00755555220454338D, -0.00949451455636883D, -0.0105154409918717D, -0.010460832484825D, -0.00937485221013864D, -0.00748779521095554D, -0.00516735944741822D, -0.00284688065681757D, -9.44144342204741E-4D, 2.14038601094618E-4D, 4.48220493090697E-4D, -2.46078432288275E-4D, -0.00170340034633871D, -0.00362421333211422D, -0.00563389951885155D, -0.00735360701803667D, -0.00846951380687305D, -0.00878816295614779D, -0.00826802752262929D, -0.00702249784257682D, -0.00529439806145769D, -0.0034077424198901D, -0.00170597869344896D, -4.88598112186092E-4D, 4.25002186405121E-5D, -1.82849419019012E-4D, -0.00109388518637658D, -0.0024958874386246D, -0.00410925127068627D, -0.00562315333865641D, -0.00675336186413425D, -0.00729374687426444D, -0.00715198450674306D, -0.00636358411753227D, -0.00508198134809249D, -0.00354738366789472D, -0.00204036227034438D, -8.29341117202341E-4D, -1.21501341208779E-4D, -2.62833612978657E-5D, -5.37603813784902E-4D, -0.00153786268861382D, -0.00282220492576368D, -0.00413837160798384D, -0.00523417542360424D, -0.00590398183887164D, -0.00602539931306045D, -0.00557995156612952D, -0.00465401417839112D, -0.00342058739997302D, -0.00210539663183083D, -9.4420432887318E-4D, -1.39079612299567E-4D, 1.78005205844006E-4D, -3.19766104447796E-5D, -7.10028821381479E-4D, -0.00171205950763413D, -0.00283736136973274D, -0.00386674837933375D, -0.0046034450441617D, -0.00490881515896487D, -0.00472678969339786D, -0.0040923984060442D, -0.00312350225351387D, -0.00199725651943966D, -9.16299236533598E-4D, -7.07582257228779E-5D, 3.96486626536007E-4D, 4.15303124638464E-4D, -3.02801614322377E-7D, -7.56362553657179E-4D, -0.00169797079597746D, -0.0026389926345583D, -0.00339713383295239D, -0.0038274287920596D, -0.00384837663407466D, -0.00345566817171523D, -0.00272159333227661D, -0.00178012805276415D, -8.01113927599773E-4D, 4.18882442035887E-5D, 6.03626371741223E-4D, 7.93553359180197E-4D, 5.91427811460621E-4D, 4.98425162577E-5D, -7.16946449325894E-4D, -0.00155516788603045D, -0.00230077814015934D, -0.00280985876267867D, -0.00298494810943401D, -0.00279210409544492D, -0.00226613307010532D, -0.00150281291712562D, -6.40203673959743E-4D, 1.67731567404481E-4D, 7.79759007376738E-4D, 0.0010933599394102D, 0.00106288579277491D, 7.07100966540956E-4D, 1.0447037192244E-4D, -6.22282918266146E-4D, -0.00132975088840251D, -0.00188060329600902D, -0.00216898578250211D, -0.00213943521160232D, -0.00179638061831811D, -0.00120218142834308D, -4.64720243596698E-4D, 2.83438836206569E-4D, 9.0986693888556E-4D, 0.00130687364779612D, 0.00141088442354037D, 0.00121357089114537D, 7.6234087929536E-4D, 1.50755304146288E-4D, -4.99669263582827E-4D, -0.00106227941833982D, -0.00142891708711331D, -0.00152949276953303D, -0.00134444209695172D, -9.07384243766655E-4D, -2.98152986619019E-4D, 3.7285572048885E-4D, 9.85476250082807E-4D, 0.00143228621248031D, 0.00163813734628643D, 0.00157365651799556D, 0.00125980163155078D, 7.63296964727054E-4D, 1.83416169919086E-4D, -3.66775820220804E-4D, -7.81692899252715E-4D, -9.82504470494768E-4D, -9.31420112254212E-4D, -6.37884782523169E-4D, -1.56208575877302E-4D, 4.25335294937135E-4D, 0.00100124647033075D, 0.00146872604881992D, 0.00174651275888789D, 0.00178971139213456D, 0.00159738209371754D, 0.0012121573011249D, 7.11525817673556E-4D, 1.93088506220769E-4D, -2.43954622688529E-4D, -5.17038609916063E-4D, -5.75386700363658E-4D, -4.08761057604636E-4D, -4.88633815893146E-5D, 4.37330250865943E-4D, 9.60366107007573E-4D, 0.00142535532397548D, 0.00174946519687166D, 0.00187719780614157D, 0.00179008298063751D, 0.00150966475862766D, 0.00109274642643676D, 6.20484195280941E-4D, 1.82850004396995E-4D, -1.37996600502361E-4D, -2.8293202316019E-4D, -2.26295960430978E-4D, 1.9730705769468E-5D, 4.08117020837066E-4D, 8.66067932635046E-4D, 0.00130904009040423D, 0.00165640715611924D, 0.00184646637631842D, 0.00184740513073318D, 0.00166288067239408D, 0.00133064261755464D, 9.15185152311649E-4D, 4.95251264637447E-4D, 1.49210026530823E-4D, -5.95892868598273E-5D, -9.41422652072306E-5D, 4.9611695770945E-5D, 3.42704502465947E-4D, 7.28840658240885E-4D, 0.00113538873580102D, 0.00148689997345125D, 0.00171926753558527D, 0.00179124501034788D, 0.00169189034302864D, 0.00144196301098922D, 0.00108976947205013D, 7.01646118277693E-4D, 3.49587730781208E-4D, 9.76836786773469E-5D, -9.44217447750788E-6D, 4.52472361597781E-5D, 2.48641831922795E-4D, 5.6019737374658E-4D, 9.20018434871427E-4D, 0.00126005466552864D, 0.00151697631774312D, 0.00164363911682995D, 0.00161772585583283D, 0.00144532357565324D, 0.00115954087382408D, 8.13805297511983E-4D, 4.71854489811893E-4D, 1.95625958665316E-4D, 3.4012343541018E-5D, 1.3849309144843E-5D, 1.35466757159849E-4D, 3.72855678084063E-4D, 6.79052557660961E-4D, 9.94960964833502E-4D, 0.00126063555002634D, 0.00142619655393134D, 0.00146107399744823D, 0.00135918714255885D, 0.00113987720111445D, 8.43769833829344E-4D, 5.25127275789142E-4D, 2.41435399013574E-4D, 4.29148478210438E-5D, -3.68288426928038E-5D, 1.34194308769052E-5D, 1.80569079914348E-4D, 4.30193817156114E-4D, 7.13031741195615E-4D, 9.74496584720574E-4D, 0.0011646592200388D, 0.00124758803351198D, 0.00120759179058643D, 0.00105200093732304D, 8.09281106655558E-4D, 5.2364554256276E-4D, 2.464637750764E-4D, 2.68551602843451E-5D, -9.75382510211861E-5D, -1.0714149456976E-4D, -3.97963442099545E-6D, 1.89211708760537E-4D, 4.33304948104277E-4D, 6.80444821457835E-4D, 8.82824811935392E-4D, 0.00100170797231571D, 0.00101425160569053D, 9.17680849208474E-4D, 7.2930778548967E-4D, 4.83230524813043E-4D, 2.23593185867299E-4D, -3.51663112894297E-6D, -1.58799772313772E-4D, -2.16702229081013E-4D, -1.70122935327124E-4D, -3.12520579316764E-5D, 1.70770556774819E-4D, 3.95685490536059E-4D, 5.99512281288621E-4D, 7.42921251348084E-4D, 7.982248319367E-4D, 7.54513329299535E-4D, 6.19220331665681E-4D, 4.16678727825662E-4D, 1.83180508682799E-4D, -3.96953994968743E-5D, -2.13033893593434E-4D, -3.07522853427615E-4D, -3.08920034466907E-4D, -2.20256642442962E-4D, -6.12246546286886E-5D, 1.35904911184024E-4D, 3.3221821078459E-4D, 4.8963925032985E-4D, 5.77738539851612E-4D, 5.79439273810329E-4D, 4.93770262973607E-4D, 3.36015790834792E-4D, 1.34455506846221E-4D, -7.48344639902692E-5D, -2.54954924592149E-4D, -3.74794893114052E-4D, -4.14892907150544E-4D, -3.70652605605774E-4D, -2.53184359458119E-4D, -8.68245287093865E-5D, 9.53324649004418E-5D, 2.57871342545317E-4D, 3.69410870378961E-4D, 4.08543598287131E-4D, 3.67434261574489E-4D, 2.53285447950829E-4D, 8.65917343263471E-5D, -1.02610485587946E-4D, -2.80623721390072E-4D, -4.16139661135309E-4D, -4.86191446429637E-4D, -4.80063170756953E-4D, -4.01313076289361E-4D, -2.66713802736679E-4D, -1.03198447100559E-4D, 5.74523943528654E-5D, 1.84316204896415E-4D, 2.5323129899956E-4D, 2.50946901093621E-4D, 1.77638742212356E-4D, 4.64904244119377E-5D, -1.18683678206045E-4D, -2.88174788977175E-4D, -4.31708214571715E-4D, -5.24244802280253E-4D, -5.50312187666917E-4D, -5.06955565217396E-4D, -4.03915938898268E-4D, -2.61904280504075E-4D, -1.08442383216512E-4D, 2.70624688829392E-5D, 1.18971164016274E-4D, 1.49870978174831E-4D, 1.13908852304928E-4D, 1.75247095049829E-5D, -1.21647686322812E-4D, -2.78364015955548E-4D, -4.24271089806838E-4D, -5.33373583604141E-4D, -5.86569142963063E-4D, -5.75313621310307E-4D, -5.02871887665066E-4D, -3.83829338777272E-4D, -2.41089628777638E-4D, -1.01816886829692E-4D, 7.8132740985481E-6D, 6.71888260706398E-5D, 6.52221969033149E-5D, 2.08912857755044E-6D, -1.10666382579362E-4D, -2.5255457870159E-4D, -3.97735963103583E-4D, -5.20018323470526E-4D, -5.97431437802573E-4D, -6.1644929580642E-4D, -5.74195668379932E-4D, -4.79138906517235E-4D, -3.49264936862277E-4D, -2.08973511855545E-4D, -8.43683732439765E-5D, 1.28683042804457E-6D, 3.20306720584621E-5D, 1.8664996724834E-6D, -8.39458869442319E-5D, -2.10096448291764E-4D, -3.5379923287896E-4D, -4.89172991016836E-4D, -5.9171601394605E-4D, -6.42981223594934E-4D, -6.33691820405254E-4D, -5.65653686021656E-4D, -4.51218621755097E-4D, -3.11303641234571E-4D, -1.71410449954226E-4D, -5.7277674807016E-5D, 9.98070420727893E-6D, 1.74584888808446E-5D, -3.69617246459944E-5D, -1.44403231408916E-4D, -2.86293068422389E-4D, -4.37843807718034E-4D, -5.72186532290299E-4D, -6.65360053537445E-4D, -7.00350679062249E-4D, -6.7039277429577E-4D, -5.80032844849327E-4D, -4.44674887956967E-4D, -2.87767026596535E-4D, -1.37026795703571E-4D, -1.94228353791984E-5D, 4.32881937489408E-5D, 3.86308033064396E-5D, -3.45312065837977E-5D, -1.65400913899566E-4D, -3.33137954084399E-4D, -5.10005871163562E-4D, -6.66185490192942E-4D, -7.74523301469025E-4D, -8.15365828849824E-4D, -7.79750742743636E-4D, -6.71317451493218E-4D, -5.05715502523139E-4D, -3.08509874719252E-4D, -1.10987588308181E-4D, 5.45352349753129E-5D, 1.60107470556599E-4D, 1.86466300968715E-4D, 1.26516426775505E-4D, -1.34624881903889E-5D, -2.14182662702644E-4D, -4.46146123317908E-4D, -6.73493754692806E-4D, -8.59260495804291E-4D, -9.70341482030938E-4D, -9.82353338460868E-4D, -8.82833781720179E-4D, -6.73193852496632E-4D, -3.68302018467704E-4D, 5.24895417994034E-6D, 4.12844215447331E-4D, 8.16034117695199E-4D, 0.00117746477759056D, 0.00146506438032912D, 0.00165575166183813D, 0.00173740932884499D, 0.00170981291692959D, 0.00158354624155544D, 0.0013781195000037D, 0.00111885157865328D, 8.33627498869421E-4D, 5.49359171310485E-4D, 2.89279202398974E-4D, 7.06479844998841E-5D, -9.60607754678275E-5D, -2.07456105101242E-4D, -2.66503975879537E-4D, -2.80928519745322E-4D, -2.61497617618105E-4D, -2.20230946712853E-4D, -1.68411968763922E-4D, -1.1588027064266E-4D, -9.7440442358423E-5D };
        private int BUFFER_SIZE_HPF = 948;
        private int filtered;
        private float[] eegBuffer_filtered = new float[this.BUFFER_SIZE_HPF];
        private float[] tempeegBuffer_filtered = new float[this.BUFFER_SIZE_HPF];
        private int bufferCounter = 0;
        private TGBlinkDetector blink;
        private TGSamplingRateCalculator sratecalc;
        private TGHrv hrv;
        private RespiratoryRate respiratory;
        private EnergyLevel energy;
        private Positivity positivity;
        private TaskFamiliarity familiarity;
        private TaskDifficulty difficulty;
        private int poorSignal = 255;
        private int rrint = 0;
        private int samplingRate = 0;
        private long timeLastInterval = -1L;
        private long timeInt;
        private int zone = 0;
        private int attention = 0;

        public TGParser(Handler handler, boolean rawDataEnable)
        {
            this.handler = handler;
            this.rawEnabled = rawDataEnable;
            this.parserState = 1;
            this.blink = new TGBlinkDetector();
            this.respiratory = new RespiratoryRate(handler);
            this.energy = new EnergyLevel();
            this.sratecalc = new TGSamplingRateCalculator();
            this.hrv = new TGHrv();
            this.positivity = new Positivity();
            this.familiarity = new TaskFamiliarity();
            this.difficulty = new TaskDifficulty();
        }

        public final int parseByte(byte currByte)
        {
            switch (this.parserState)
            {
                case 1:
                    if ((currByte & 0xFF) == 170) {
                        this.parserState = 2;
                    } else if (TGDevice.this.logEnabled) {
                        TGDevice.access$1000(TGDevice.this, "Waiting for sync");
                    }
                    break;
                case 2:
                    if ((currByte & 0xFF) == 170)
                    {
                        this.parserState = 3;
                    }
                    else
                    {
                        this.parserState = 1;
                        if (TGDevice.this.logEnabled) {
                            TGDevice.access$1000(TGDevice.this, "Bad 2nd sync byte");
                        }
                    }
                    break;
                case 3:
                    this.payloadLength = (currByte & 0xFF);
                    if (this.payloadLength < 170)
                    {
                        this.payloadBytesReceived = 0;
                        this.payloadSum = 0;
                        this.parserState = 4;
                    }
                    else if (this.payloadLength == 170)
                    {
                        this.parserState = 2;
                    }
                    else
                    {
                        this.parserState = 1;
                    }
                    break;
                case 4:
                    this.payload[(this.payloadBytesReceived++)] = currByte;
                    this.payloadSum += (currByte & 0xFF);
                    if (this.payloadBytesReceived == this.payloadLength) {
                        this.parserState = 5;
                    }
                    break;
                case 5:
                    this.checksum = (currByte & 0xFF);
                    if (this.checksum == ((this.payloadSum ^ 0xFFFFFFFF) & 0xFF))
                    {
                        int i,k,m,n,i1,i2,i3,i4,i5,i6;
                        if (this.handler != null)
                        {
                            currByte = this;
                            for (i6 = 0; i6 < currByte.payloadLength;)
                            {
                                i = currByte.payload[i6] & 0xFF;i6++;
                                if (i > 127)
                                {
                                    k = currByte.payload[i6] & 0xFF;i6++;
                                }
                                else
                                {
                                    k = 1;
                                }
                                Object localObject2;
                                Object localObject1;
                                int j;
                                switch (i)
                                {
                                    case 1:
                                        currByte.handler.obtainMessage(20).sendToTarget();
                                        break;
                                    case 2:
                                        if (currByte.rawCountEnabled)
                                        {
                                            currByte.handler.obtainMessage(19,
                                                    currByte.rawCount, 0).sendToTarget();
                                            TGDevice.access$1000(currByte.this$0,
                                                    "Raw Count: "
                                                    + currByte.rawCount);
                                            currByte.rawCount = 0;
                                        }
                                        if ((currByte.trimByteSent)
                                                && (currByte.poorSignal == 200)
                                                && ((currByte.payload[i6] & 0xFF) == 0)) {
                                            currByte.hrv.Reset();
                                            currByte.this$0.rrintBuffer.clear();
                                            currByte.this$0.rrintBufferForHeartAge.clear();
                                            currByte.this$0.inputRawBuffer.clear();
                                            TGDevice.access$1402(0);
                                            currByte.calculationEnabled = false;
                                            for (int i7 = 0; i7 < currByte.BUFFER_SIZE_HPF; i7++){
                                                currByte.tempeegBuffer_filtered[i7] = 0.0F;
                                                currByte.eegBuffer_filtered[i7] = 0.0F;
                                            }
                                        }
                                        currByte.poorSignal =
                                                (currByte.payload[i6] & 0xFF);
                                        currByte.handler
                                                .obtainMessage(2, currByte.poorSignal, 0)
                                                .sendToTarget();
                                        if (currByte.this$0.logEnabled) {
                                            TGDevice.access$1000(currByte.this$0, "02 01 "
                                                    + String.format("%02X",
                                                    new Object[] {
                                                            Integer.valueOf(
                                                                    currByte.payload[i6] & 0xFF) })
                                                    + " PoorSignal: " + currByte.poorSignal);
                                        }
                                        i6 += k; break;
                                    case 4:
                                        currByte.attention =
                                                (currByte.payload[i6] & 0xFF);
                                        currByte.handler
                                                .obtainMessage(4, currByte.attention, 0)
                                                .sendToTarget();
                                        if (currByte.this$0.logEnabled) {
                                            TGDevice.access$1000(
                                                    currByte.this$0,
                                                    "04 01 "
                                                     + String.format("%02X",
                                                            new Object[] {
                                                            Integer.valueOf(
                                                                    currByte.payload[i6] & 0xFF) })
                                                            + " Attention: " + (currByte.payload[i6] & 0xFF));
                                        }
                                        i6 += k;
                                        break;
                                    case 5:
                                        currByte.handler
                                                .obtainMessage(5, currByte.payload[i6] & 0xFF, 0).sendToTarget();
                                        currByte.zone = Meditation.getZone(currByte.attention, currByte.payload[i6]);
                                        currByte.handler.obtainMessage(14, currByte.zone, 0).sendToTarget();
                                        if (currByte.this$0.logEnabled) {
                                            TGDevice.access$1000(currByte.this$0,
                                                    "05 01 " + String.format("%02X",
                                                            new Object[] { Integer.valueOf(currByte.payload[i6] & 0xFF) })
                                                            + " Meditation: " + (currByte.payload[i6] & 0xFF));
                                        }
                                        i6 += k;
                                        break;
                                    case 3:
                                        i6 += k;
                                        break;
                                    case 128:
                                        if ((i = (currByte.payload[i6] << 8) + currByte.payload[(i6 + 1)]) > 32768) {
                                            i -= 65536;
                                        }
                                        if (TGDevice.ekgPersonalizationEnabled) {
                                            currByte.this$0.ekgSenseMan.addSample(i, currByte.poorSignal);
                                        }
                                        System.arraycopy(currByte.eegBuffer_filtered, 1,
                                                currByte.tempeegBuffer_filtered, 0, currByte.BUFFER_SIZE_HPF - 1);
                                        currByte.tempeegBuffer_filtered[(currByte.BUFFER_SIZE_HPF - 1)] = i;
                                        System.arraycopy(currByte.tempeegBuffer_filtered,
                                                0, currByte.eegBuffer_filtered,
                                                0, currByte.BUFFER_SIZE_HPF);
                                        currByte.bufferCounter += 1;
                                        if (currByte.bufferCounter >= currByte.BUFFER_SIZE_HPF)
                                        {
                                            currByte.filtered = currByte.applyFilter(currByte.eegBuffer_filtered);
                                            currByte.bufferCounter = (currByte.BUFFER_SIZE_HPF - 1);
                                        }
                                        if (currByte.rawEnabled)
                                        {
                                            currByte.handler.obtainMessage(128, i, 0).sendToTarget();
                                            if (currByte.this$0.logEnabled) {
                                                TGDevice.access$1000(currByte.this$0,
                                                        "80 02 " + String.format("%02X",
                                                                new Object[] {
                                                                        Integer.valueOf(currByte.payload[i6] & 0xFF) })
                                                                + " " + String.format("%02X", new Object[] {
                                                                Integer.valueOf(currByte.payload[(i6 + 1)] & 0xFF) })
                                                                + " Raw: " + i);
                                            }
                                        }
                                        if (currByte.rawCountEnabled) {
                                            currByte.rawCount += 1;
                                        }
                                        int i9;
                                        if ((currByte.trimByteSent) && (currByte.poorSignal == 200))
                                        {
                                            TGDevice.access$1408();
                                            if (TGDevice.pass_raw_counter >= currByte.this$0.pass_seconds << 9) {
                                                currByte.calculationEnabled = true;
                                            }
                                            currByte.rrint = currByte.hrv.AddData(currByte.filtered);
                                            if (currByte.rrint > 0)
                                            {
                                                if (currByte.timeBasedIntervalEnabled) {
                                                    if (currByte.timeLastInterval == -1L)
                                                    {
                                                        currByte.timeLastInterval = System.currentTimeMillis();
                                                    }
                                                    else
                                                    {
                                                        currByte.timeInt
                                                                = (System.currentTimeMillis() - currByte.timeLastInterval);
                                                        if ((currByte.timeInt > -2147483648L)
                                                                && (currByte.timeInt < 2147483647L)) {
                                                            currByte.rrint = ((int)currByte.timeInt);
                                                        } else {
                                                            currByte.rrint = ((int)Math.round(currByte.rrint / 0.512D));
                                                        }
                                                        currByte.timeLastInterval = System.currentTimeMillis();
                                                        break label1522;
                                                    }
                                                }
                                                currByte.rrint =
                                                        ((int)Math.round(currByte.rrint / 0.512D));
                                                currByte.handler.obtainMessage(271, currByte.rrint, 0).sendToTarget();
                                                if (currByte.calculationEnabled)
                                                {
                                                    currByte.this$0.rrintBuffer.add(Integer.valueOf(currByte.rrint));
                                                    currByte.this$0.rrintBufferForHeartAge.add(Integer.valueOf(currByte.rrint));
                                                    Integer[] arrayOfInteger;
                                                    if (currByte.this$0.rrintBufferForHeartAge.size() == 70)
                                                    {
                                                        arrayOfInteger = new Integer[70];
                                                        for (i9 = 0; i9 < 70; i9++) {
                                                            arrayOfInteger[i9] =
                                                                    ((Integer)currByte.this$0.rrintBufferForHeartAge.get(i9));
                                                        }
                                                        if (currByte.this$0.inputAge > 0)
                                                        {
                                                            i9 = (int)NeuroSkyHeartMeters
                                                                    .calculateHeartAge(arrayOfInteger,
                                                                            currByte.this$0.inputAge, 16);
                                                            currByte.handler.obtainMessage(272, i9, 0).sendToTarget();
                                                        }
                                                        currByte.this$0.rrintBufferForHeartAge.clear();
                                                    }
                                                    if (currByte.this$0.rrintBuffer.size() == 300)
                                                    {
                                                        arrayOfInteger = new Integer['?'];
                                                        for (i9 = 120; i9 < 300; i9++) {
                                                            arrayOfInteger[(i9 - 120)] =
                                                                    ((Integer)currByte.this$0.rrintBuffer.get(i9));
                                                        }
                                                        if (currByte.this$0.inputAge > 0)
                                                        {
                                                            i9 = (int)NeuroSkyHeartMeters
                                                                    .calculateHeartAge(arrayOfInteger,
                                                                            currByte.this$0.inputAge, 8);
                                                            currByte.handler.obtainMessage(273, i9, 0).sendToTarget();
                                                        }
                                                    }
                                                }
                                                double d1 =
                                                        Math.round(d1 = 60.0D / (currByte.rrint / 1000.0D));
                                                currByte.handler.obtainMessage(3, (int)d1, 0).sendToTarget();
                                            }
                                        }
                                        int i8;
                                        if ((currByte.trimByteSent)
                                                && (currByte.rrint > 0)
                                                && (currByte.relaxationEnabled)
                                                && (currByte.calculationEnabled)
                                                && ((i8 = currByte.energy.addInterval(currByte.rrint,
                                                currByte.poorSignal)) > 0)) {
                                            currByte.handler.obtainMessage(24, i8, 0).sendToTarget();
                                        }
                                        if ((currByte.trimByteSent) && (currByte.respirationEnabled)) {
                                            currByte.respiratory.calculateRespiratoryRate((short)i, currByte.poorSignal);
                                        }
                                        currByte.this$0.inputRawBuffer.add(Integer.valueOf(i));
                                        if (currByte.this$0.inputRawBuffer.size() == 30720)
                                        {
                                            localObject2 = new double['��'];
                                            for (i9 = 0; i9 < 30720; i9++) {
                                                localObject2[i9] =
                                                        ((Integer)currByte.this$0.inputRawBuffer.get(i9)).intValue();
                                            }
                                        }
                                        i6 += k; break;
                                    case 131:
                                        if (k == 24)
                                        {
                                            i = getEEGPowerValue(currByte.payload[i6],
                                                    currByte.payload[(i6 + 1)],
                                                    currByte.payload[(i6 + 2)]);
                                            m = getEEGPowerValue(currByte.payload[(i6 + 3)],
                                                    currByte.payload[(i6 + 4)],
                                                    currByte.payload[(i6 + 5)]);
                                            n = getEEGPowerValue(currByte.payload[(i6 + 6)],
                                                    currByte.payload[(i6 + 7)],
                                                    currByte.payload[(i6 + 8)]);
                                            i1 = getEEGPowerValue(currByte.payload[(i6 + 9)],
                                                    currByte.payload[(i6 + 10)],
                                                    currByte.payload[(i6 + 11)]);
                                            i2 = getEEGPowerValue(currByte.payload[(i6 + 12)],
                                                    currByte.payload[(i6 + 13)],
                                                    currByte.payload[(i6 + 14)]);
                                            i3 = getEEGPowerValue(currByte.payload[(i6 + 15)],
                                                    currByte.payload[(i6 + 16)],
                                                    currByte.payload[(i6 + 17)]);
                                            i4 = getEEGPowerValue(currByte.payload[(i6 + 18)],
                                                    currByte.payload[(i6 + 19)],
                                                    currByte.payload[(i6 + 20)]);
                                            i5 = getEEGPowerValue(currByte.payload[(i6 + 21)],
                                                    currByte.payload[(i6 + 22)],
                                                    currByte.payload[(i6 + 23)]);
                                            localObject2 = new TGEegPower(i, m, n, i1, i2, i3, i4, i5);
                                            currByte.handler.obtainMessage(131, localObject2).sendToTarget();
                                            if (currByte.this$0.logEnabled) {
                                                TGDevice.access$1000(currByte.this$0,
                                                        "83 18 " + String.format("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X", new Object[] { Byte.valueOf(currByte.payload[i6]), Byte.valueOf(currByte.payload[(i6 + 1)]), Byte.valueOf(currByte.payload[(i6 + 2)]), Byte.valueOf(currByte.payload[(i6 + 3)]), Byte.valueOf(currByte.payload[(i6 + 4)]), Byte.valueOf(currByte.payload[(i6 + 5)]), Byte.valueOf(currByte.payload[(i6 + 6)]), Byte.valueOf(currByte.payload[(i6 + 7)]), Byte.valueOf(currByte.payload[(i6 + 8)]), Byte.valueOf(currByte.payload[(i6 + 9)]), Byte.valueOf(currByte.payload[(i6 + 10)]), Byte.valueOf(currByte.payload[(i6 + 11)]), Byte.valueOf(currByte.payload[(i6 + 12)]), Byte.valueOf(currByte.payload[(i6 + 13)]), Byte.valueOf(currByte.payload[(i6 + 14)]), Byte.valueOf(currByte.payload[(i6 + 15)]), Byte.valueOf(currByte.payload[(i6 + 16)]), Byte.valueOf(currByte.payload[(i6 + 17)]), Byte.valueOf(currByte.payload[(i6 + 18)]), Byte.valueOf(currByte.payload[(i6 + 19)]), Byte.valueOf(currByte.payload[(i6 + 20)]), Byte.valueOf(currByte.payload[(i6 + 21)]), Byte.valueOf(currByte.payload[(i6 + 22)]), Byte.valueOf(currByte.payload[(i6 + 23)]) }));
                                            }
                                        }
                                        i6 += k; break;
                                    case 144:
                                        (localObject1 = new TGRawMulti())
                                                .ch1 = ((currByte.payload[i6] << 8) + currByte.payload[(i6 + 1)]);
                                        if (((TGRawMulti)localObject1).ch1 > 32768) {
                                            localObject1.ch1 -= 65536;
                                        }
                                        if (k > 3)
                                        {
                                            ((TGRawMulti)localObject1).ch2 =
                                                    ((currByte.payload[(i6 + 3)] << 8) + currByte.payload[(i6 + 4)]);
                                            if (((TGRawMulti)localObject1).ch2 > 32768) {
                                                localObject1.ch2 -= 65536;
                                            }
                                            if (k > 6)
                                            {
                                                ((TGRawMulti)localObject1).ch3 =
                                                        ((currByte.payload[(i6 + 6)] << 8) + currByte.payload[(i6 + 7)]);
                                                if (((TGRawMulti)localObject1).ch3 > 32768) {
                                                    localObject1.ch3 -= 65536;
                                                }
                                                if (k > 9)
                                                {
                                                    ((TGRawMulti)localObject1).ch4 =
                                                            ((currByte.payload[(i6 + 9)] << 8) + currByte.payload[(i6 + 10)]);
                                                    if (((TGRawMulti)localObject1).ch4 > 32768) {
                                                        localObject1.ch4 -= 65536;
                                                    }
                                                    if (k > 12)
                                                    {
                                                        ((TGRawMulti)localObject1).ch5 =
                                                                ((currByte.payload[(i6 + 12)] << 8) + currByte.payload[(i6 + 13)]);
                                                        if (((TGRawMulti)localObject1).ch5 > 32768) {
                                                            localObject1.ch5 -= 65536;
                                                        }
                                                        if (k > 15)
                                                        {
                                                            ((TGRawMulti)localObject1).ch6
                                                                    = ((currByte.payload[(i6 + 15)] << 8) + currByte.payload[(i6 + 16)]);
                                                            if (((TGRawMulti)localObject1).ch6 > 32768) {
                                                                localObject1.ch6 -= 65536;
                                                            }
                                                            if (k > 18)
                                                            {
                                                                ((TGRawMulti)localObject1).ch7 = ((currByte.payload[(i6 + 18)] << 8) + currByte.payload[(i6 + 19)]);
                                                                if (((TGRawMulti)localObject1).ch7 > 32768) {
                                                                    localObject1.ch7 -= 65536;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        currByte.handler.obtainMessage(145, localObject1).sendToTarget();
                                        double d2;
                                        if ((d2 = currByte.positivity.detect((byte)currByte.poorSignal, (short)((TGRawMulti)localObject1).ch1, (short)((TGRawMulti)localObject1).ch2)) != 200.0D) {
                                            currByte.handler.obtainMessage(26, Double.valueOf(d2)).sendToTarget();
                                        }
                                        i6 += k; break;
                                    case 177:
                                        (localObject1 = new TGRawMulti()).timeStamp1 = ((short)currByte.payload[i6] & 0xFF);((TGRawMulti)localObject1).ch1 = ((short)((currByte.payload[(i6 + 1)] << 8) + currByte.payload[(i6 + 2)]));
                                        if (((TGRawMulti)localObject1).ch1 > 32768) {
                                            localObject1.ch1 -= 65536;
                                        }
                                        ((TGRawMulti)localObject1).ch2 = ((short)((currByte.payload[(i6 + 3)] << 8) + currByte.payload[(i6 + 4)]));
                                        if (((TGRawMulti)localObject1).ch2 > 32768) {
                                            localObject1.ch2 -= 65536;
                                        }
                                        ((TGRawMulti)localObject1).ch3 = ((currByte.payload[(i6 + 5)] << 8) + currByte.payload[(i6 + 6)]);
                                        if (((TGRawMulti)localObject1).ch3 > 32768) {
                                            localObject1.ch3 -= 65536;
                                        }
                                        ((TGRawMulti)localObject1).ch4 = ((currByte.payload[(i6 + 7)] << 8) + currByte.payload[(i6 + 8)]);
                                        if (((TGRawMulti)localObject1).ch4 > 32768) {
                                            localObject1.ch4 -= 65536;
                                        }
                                        ((TGRawMulti)localObject1).ch5 = ((currByte.payload[(i6 + 9)] << 8) + currByte.payload[(i6 + 10)]);
                                        if (((TGRawMulti)localObject1).ch5 > 32768) {
                                            localObject1.ch5 -= 65536;
                                        }
                                        ((TGRawMulti)localObject1).ch6 = ((currByte.payload[(i6 + 11)] << 8) + currByte.payload[(i6 + 12)]);
                                        if (((TGRawMulti)localObject1).ch6 > 32768) {
                                            localObject1.ch6 -= 65536;
                                        }
                                        ((TGRawMulti)localObject1).ch7 = ((currByte.payload[(i6 + 13)] << 8) + currByte.payload[(i6 + 14)]);
                                        if (((TGRawMulti)localObject1).ch7 > 32768) {
                                            localObject1.ch7 -= 65536;
                                        }
                                        ((TGRawMulti)localObject1).ch8 = ((currByte.payload[(i6 + 15)] << 8) + currByte.payload[(i6 + 16)]);
                                        if (((TGRawMulti)localObject1).ch8 > 32768) {
                                            localObject1.ch8 -= 65536;
                                        }
                                        currByte.handler.obtainMessage(177, localObject1).sendToTarget();i6 += k; break;
                                    case 8:
                                        if ((!currByte.trimByteSent) && (!currByte.isBMD101))
                                        {
                                            localObject1 = new byte[] { -86, -86, 4, 3, 64, -7, 0, (byte)currByte.payload[i6] };TGDevice.access$1500(currByte.this$0, (byte[])localObject1);Log.v("TGDevice", "Sent trim byte: " + currByte.payload[i6]);currByte.trimByteSent = true;
                                        }
                                        i6 += k; break;
                                    case 132:
                                    case 176:
                                        i6 += k; break;
                                    case 133:
                                        if ((currByte.payload[(i6 + 2)] & 0xF0) == 240) {
                                            currByte.isBMD101 = false;
                                        } else if (!currByte.trimByteSent) {
                                            currByte.trimByteSent = true;
                                        }
                                        i6 += k; break;
                                    case 179:
                                        j = currByte.payload[i6] & 0xFF;m = currByte.payload[(i6 + 1)] & 0xFF;Log.v("TGDevice", "Poorsignal1: " + j + " ps2: " + m);currByte.handler.obtainMessage(179, j, m).sendToTarget();
                                        if ((j == 200) && (m == 200)) {
                                            currByte.poorSignal = 200;
                                        } else {
                                            currByte.poorSignal = 0;
                                        }
                                        i6 += k; break;
                                    default:
                                        Log.v("TGParser", "Unknown code: " + j);
                                }
                            }
                        }
                        label1522:
                        this.parserState = 1;
                        return 1;
                    }
                    if (this.checksum == 170)
                    {
                        this.parserState = 2;
                        return -2;
                    }
                    Log.d("TGDevice", "Checksum failed. Recieved: " + this.checksum + ". Calculated: " + ((this.payloadSum ^ 0xFFFFFFFF) & 0xFF));
                    if (TGDevice.this.logEnabled) {
                        TGDevice.access$1000(TGDevice.this, "Checksum failed. Received: " + this.checksum + ". Calculated: " + ((this.payloadSum ^ 0xFFFFFFFF) & 0xFF));
                    }
                    this.parserState = 1;
                    return -2;
            }
            return this.parserState;
        }

        private int applyFilter(float[] data)
        {
            int length = data.length;
            int result = 0;
            for (int i = 0; i < length; i++) {
                result += (int)(data[i] * this.coeff[i]);
            }
            return result;
        }

        private static int getEEGPowerValue(byte highOrderByte, byte middleOrderByte, byte lowOrderByte)
        {
            return (highOrderByte << 16) + (middleOrderByte << 8) + lowOrderByte & 0xFFFFFF;
        }
    }

    public double getHeartAge()
    {
        if (this.rrintBuffer.size() == 0) {
            return -1.0D;
        }
        Integer[] t = new Integer[this.rrintBuffer.size()];
        double d;
        return d = NeuroSkyHeartMeters.calculateHeartAgeRaw((Integer[])this.rrintBuffer.toArray(t));
    }

    public int getHeartRisk()
    {
        if (this.rrintBuffer.size() == 0) {
            return -1;
        }
        Integer[] t = new Integer[this.rrintBuffer.size()];

        return t = NeuroSkyHeartMeters.calculateHeartRiskAware((Integer[])this.rrintBuffer.toArray(t));
    }

    public int EKGloadParams(String name)
    {
        if (!ekgPersonalizationEnabled) {
            return -1;
        }
        if (name.length() == 0)
        {
            Log.v("TGDevice", "EKG Mode: Indentificiation");
            this.ekgSenseMan.ekgSenseObj.params = new EkgParameters();
            return 0;
        }
        Log.v("TGDevice", "EKG Mode: Authentication");
        this.ekgSenseMan.loadParameterFile(name);
        return 1;
    }

    public void EKGstartDetection()
    {
        if (!ekgPersonalizationEnabled) {
            return;
        }
        this.ekgSenseMan.resetBuffer();
        this.ekgSenseMan.ekgSenseObj.currentData = new EkgTemplate();
        this.ekgState = 3;
    }

    public void EKGstopDetection()
    {
        if (!ekgPersonalizationEnabled) {
            return;
        }
        this.ekgState = 0;
    }

    /**
     * @deprecated
     */
    public void EKGstartTraining(String userName)
    {
        if (!ekgPersonalizationEnabled) {
            return;
        }
        this.ekgState = 1;
        this.ekgSenseMan.learnName = userName;
    }

    public void EKGstartLongTraining(String userName)
    {
        if (!ekgPersonalizationEnabled) {
            return;
        }
        this.ekgState = 2;
        this.ekgSenseMan.ekgSenseObj.reset();
        this.ekgSenseMan.learnName = userName;
        this.ekgSenseMan.learnCounter = 0;
    }

    public void EKGstopTraining()
    {
        if (!ekgPersonalizationEnabled) {
            return;
        }
        this.ekgState = 0;
        this.ekgSenseMan.learnName = "";
    }

    public boolean EKGdeleteUser(String name)
    {
        if (!ekgPersonalizationEnabled) {
            return false;
        }
        File dir;
        String[] fileNames;
        if ((fileNames = (dir = new File(Environment.getExternalStorageDirectory()
                + "/Android/data/com.neurosky.thinkgear/files/EKG/templates")).list()) != null)
        {
            String[] arr$;
            int len$ = (arr$ = fileNames).length;
            for (int i$ = 0; i$ < len$; i$++)
            {
                String n;
                if ((n = arr$[i$]).startsWith(name + ".json"))
                {
                    File localFile1;
                    if ((localFile1 = new File(dir, n)).delete())
                    {
                        Log.v("TGDevice", "Deleted file: " + n);
                        this.ekgSenseMan.loadTemplates();
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
